/*
 * @Description: svg-icon 全局按需注册
 * @Author: HuiSir<273250950@qq.com>
 * @Date: 2021-11-01 19:03:38
 * @LastEditTime: 2025-07-10 17:49:59
 */

/* 使用vue插件方式导出组件，插件API：https://v3.cn.vuejs.org/guide/plugins.html */
import { App, Component } from "vue";
import { useCurrentInstance } from "../helpers/hooks";

/**
 * element-plus/icons
 */
import * as elementIcons from "@element-plus/icons-vue";

/**
 * 自定义icon
 */
const myIcons = import.meta.glob("./inc/*.vue", { eager: true });

export default (app: App) => {
  // 所有icon
  const Icons: string[] = [];

  // element-icon注册
  for (const elIconsName in elementIcons) {
    app.component(
      `Icon${elIconsName}`,
      (elementIcons as Record<string, Component>)[elIconsName]
    );
    Icons.push(elIconsName);
  }

  // 自定义icon注册
  for (const path in myIcons) {
    const comp = myIcons[path] as { default: CompDefaultOpt };
    app.component(`Icon${comp.default.name}`, comp.default);
    Icons.push(comp.default.name!);
  }

  // 缓存所有图标用于菜单图标选择
  useCurrentInstance(app).globalProperties.$getIcons = () => Icons;

  return app;
};
